
--[[
	消息处理

--]]



---------------------------------------------------------
-- class
---------------------------------------------------------
local M = class("Handler")

function M:ctor()
    self:register()
end


function M:register()
    g.register("C2_SN_hit_ball", handler(self, self.reqHitBall))

    g.register("C2_SN_sync_ball", handler(self, self.reqSyncBall))
    g.register("C2_SN_ball_stop", handler(self, self.reqBallStop))
    g.register("C2_SN_ball_goin", handler(self, self.reqBallGoin))
    --g.register("C2_SN_first_ball", handler(self, self.reqFirstBall))
    g.register("C2_SN_sync_pole", handler(self, self.reqSyncPole))
    g.register("C2_SN_sync_slider", handler(self, self.reqSyncSlider))


end


function M:reqHitBall(msg)
    g.log:info("reqHitBall:", msg.mid, msg)
    local uid = msg.mid
    local user = g.user_mgr:getUser(uid)
    if not user then
        return { code = g.error_code.invalid_mid,}
    end
    user:resetOperNum()
    --只有在play状态才起作用
    if g.desk:getStage() ~= g.desk.s_stage_play then
        return { code = g.error_code.input_err,}
    end
    if uid ~= g.desk.cur_oper:getUid() then
        g.log:deskErr(uid, "reqHitBall", g.desk.cur_oper:getUid())
        return { code = g.error_code.input_err,}
    end
    --打球后应该中断定时器
    g.desk:onHitBall(uid)

    --转发给别的玩家
    g.desk:sendOther(uid, "SN_2C_hit_ball", msg)


    return {
        code = 0,
    }
end

function M:reqSyncBall(msg)
    --g.log:info(msg.mid, msg)
    local uid = msg.mid
    --消息验证
    local user = g.user_mgr:getUser(uid)
    if not user then
        return { code = g.error_code.invalid_mid,}
    end
    --只有在move状态才起作用
    if g.desk:getStage() ~= g.desk.s_stage_move then
        return { code = 0,}
    end
    if not g.desk:checkSyncBall(msg) then
        return
    end
    --转发给别的玩家
    g.desk:sendOther(uid, "SN_2C_sync_ball", msg)

    return {
        code = 0,
    }
end

function M:reqBallStop(msg)
    g.log:debug("reqBallStop", msg)
    local uid = msg.mid
    --消息验证
    local user = g.user_mgr:getUser(uid)
    if not user then
        return { code = g.error_code.invalid_mid,}
    end
    --只有在move状态才起作用
    if g.desk:getStage() ~= g.desk.s_stage_move then
        print("reqBallStop: state need move", g.desk:getStage())
        return { code = 0,}
    end
    if uid ~= g.desk.cur_oper:getUid() and g.desk.cur_oper:isPlayer() then
        --如果一个玩家掉线了，则使用别一个玩家的进球信息
        if g.desk.cur_oper:isOnline() then
            return {code = 0}
        end
        g.log:deskInfo(uid, "reqBallStop cur_oper disconnect", g.desk.cur_oper:getUid())
    end
    --球停止后让下一个操作者打球
    g.desk:onBallStop(msg)

    return {
        code = 0,
    }
end

function M:reqBallGoin(msg)
    --g.log:err(msg.mid, msg)
    local uid = msg.mid
    --消息验证
    local user = g.user_mgr:getUser(uid)
    if not user then
        return { code = g.error_code.invalid_mid,}
    end
    local uid = msg.mid
    if uid ~= g.desk.cur_oper:getUid() and g.desk.cur_oper:isPlayer() then
        --如果一个玩家掉线了，则使用别一个玩家的进球信息
        if g.desk.cur_oper:isOnline() then
            --g.log:deskErr(uid, "reqBallGoin", g.desk.cur_oper:getUid())
            --return { code = g.error_code.input_err,}
            return {code = 0}
        end
        g.log:deskInfo(uid, "reqBallGoin cur_oper disconnect", g.desk.cur_oper:getUid())
    end
    --只有在move状态才起作用
    if g.desk:getStage() ~= g.desk.s_stage_move then
        --print("reqBallGoin111111111111", g.desk:getStage())
        return { code = g.error_code.input_err,}
    end
    --球进洞后，可以继续操作
    g.desk:onBallGoin(msg)

    return { code = 0, balls}
end
--
--function M:reqFirstBall(msg)
--    print("reqFirstBall", msg)
--    local uid = msg.mid
--    local user = g.user_mgr:getUser(uid)
--    if not user then
--        return { code = g.error_code.invalid_mid,}
--    end
--    if uid ~= g.desk.cur_oper:getUid() and g.desk.cur_oper:isPlayer() then
--        g.log:deskErr(uid, "reqBallGoin", g.desk.cur_oper:getUid())
--        return { code = 0,}
--    end
--    --只有在move状态才起作用
--    if g.desk:getStage() ~= g.desk.s_stage_move then
--        return { code = 0,}
--    end
--    local cur_uid = g.desk.cur_oper:getUid()
--    local user = g.user_mgr:getUser(cur_uid)
--    local kind = 0
--    local ball = msg.ball
--    if ball.tag >= 11 then
--        kind = 1
--    end
--    user:setCamp(kind)
--    local rival = g.desk:getRiVal(cur_uid)
--    rival:setCamp(1-kind)
--
--    return { code = 0,}
--end

function M:reqSyncPole(msg)
    g.log:debug("reqSyncPole:", msg)
    local uid = msg.mid
    --消息验证
    if not g.desk.cur_oper then
        g.log:warn("reqSyncPole:", g.desk:getStage())
        return { code = g.error_code.input_err,}
    end
    local user = g.user_mgr:getUser(uid)
    if not user or uid ~= g.desk.cur_oper:getUid() then
        return { code = g.error_code.invalid_mid,}
    end
    user:resetOperNum()
    --转发给别的玩家
    g.desk:sendOther(uid, "SN_2C_sync_pole", msg)

    return {
        code = 0,
    }

end
function M:reqSyncSlider(msg)
    g.log:debug("reqSyncSlider:", msg)
    local uid = msg.mid
    --消息验证
    if not g.desk.cur_oper then
        g.log:warn("reqSyncSlider:", g.desk:getStage())
        return { code = g.error_code.input_err,}
    end
    local user = g.user_mgr:getUser(uid)
    if not user or uid ~= g.desk.cur_oper:getUid() then
        return { code = g.error_code.invalid_mid,}
    end
    user:resetOperNum()
    --转发给别的玩家
    g.desk:sendOther(uid, "SN_2C_sync_slider", msg)
    return {
        code = 0,
    }

end


return M